package com.zhledu.course.string;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class Person {
    //属性使用private修饰(+2分)
    private String idCard;
    private String name;

    //自定义构造方法（+2分）
    public Person(String idCard) {
        this.idCard = idCard;
    }

    //只为idCard提供get方法（+1分）
    public String getIdCard() {
        return idCard;
    }

    //为name提供get和set方法（+1分）
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //返回生日（+2分）
    public Date getBirthday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try {
            return sdf.parse(idCard.substring(6, 14));
        } catch (Exception e) {
            return null;
        }
    }

    //返回年龄（+2分）
    public int getAge() {
        return new Date().getYear() + 1900 - Integer.parseInt(idCard.substring(6, 10));
    }

    //返回性别(+2分)
    public String getSex() {
        return idCard.charAt(16) % 2 == 1 ? "男" : "女";
    }

    public int sum(int n) {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        return sum;
    }

    public void sort(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            int index = i;
            int max_min = a[index];
            for (int j = i + 1; j < a.length; j++) {
                if (max_min > a[j]) {
                    index = j;
                }
            }
            if (index != i) {
                int tmp = a[i];
                a[i] = a[index];
                a[index] = tmp;
            }
        }
    }

    //写出测试方法（+2分）
    public static void main(String[] args) {
        Person person = new Person("420602198607214518");
        System.out.println(person.getIdCard());
        System.out.println(person.getSex());
        System.out.println(person.getBirthday().toLocaleString());
        System.out.println(person.getAge());

        int a[] = {1, 6, 2};
        person.sort(a);
        System.out.println(Arrays.toString(a));
    }
}
